/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actor.weapons;

import Actor.Actor;
import Actor.Projectile;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.util.Iterator;
import java.util.LinkedList;
import javax.vecmath.Point2d;

/**
 *
 * @author Eric-Standard
 */
public class Rocket extends Projectile {

    private Rectangle rect = new Rectangle();
    //Used for smoke trail
    private LinkedList<Point2d> prevPoints = new LinkedList<Point2d>();
    private static final int MAX_EXPLOSION_RADIUS = 100;
    private static final int DEFAULT_SPEED = 500;

    public Rocket(Actor owner) {
        super(owner);
        fuseTime = 4000;
        this.damage = 75;
        this.angle = owner.getAngle();
        pos = (Point2d) owner.getPos().clone();
        vel.x = Math.cos(angle);
        vel.y = Math.sin(angle);
        vel.normalize();
        vel.scale(DEFAULT_SPEED);
        rect.height = 10;
        rect.width = 10;
    }

    @Override
    public void collide(Actor a) {
        this.isActive = false;
        this.collisionDetection = false;
        Explosion explosion = new Explosion(pos);
        explosion.setMaxRadius(MAX_EXPLOSION_RADIUS);
        world.add(explosion);
    }

    @Override
    public void collide(Shape a) {
        this.isActive = false;
        this.collisionDetection = false;
        Explosion explosion = new Explosion(pos);
        explosion.setMaxRadius(MAX_EXPLOSION_RADIUS);
        world.add(explosion);
    }

    @Override
    public void act(long dt) {
        //Add to previous points array
        prevPoints.addFirst((Point2d) pos.clone());
        while (prevPoints.size() > 10) {
            prevPoints.removeLast();
        }
        super.act(dt);
        if (fuseTime < 0) {
            isActive = false;
        }
    }

    @Override
    public void draw(Graphics g) {
        //Draw rocket
        g.setColor(Color.WHITE);
        g.fillRect((int) pos.x, (int) pos.y, 10, 10);
        //Draw smoke trail
        g.setColor(Color.GRAY);
        int index = 0;
        Iterator<Point2d> iterator = prevPoints.iterator();
        while (iterator.hasNext()) {
            index++;
            Point2d point = iterator.next();
            //g.fillRect((int) point.x, (int) point.y, 10 + index, 10 + index);
            g.fillOval((int) point.x, (int) point.y, 10 + index, 10 + index);
        }
    }

    @Override
    public Shape getShape() {
        rect.x = (int) pos.x;
        rect.y = (int) pos.y;
        return rect;
    }

    @Override
    public Rectangle2D getBoundingRectangle() {
        return null;
    }
}
